//
//  ImportViewController.m
//  笔记1
//
//  Created by liujianlong on 2022/11/7.
//

#import "ImportViewController.h"
#import "ImportCell.h"
@interface ImportViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) Note *selectFile;
@property (nonatomic, assign) NSInteger index;

@end

@implementation ImportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 1000000;
    [self.collectionView registerNib:[ImportCell nib] forCellWithReuseIdentifier:[ImportCell ID]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [NotifyTool post:notifyNoteUpdate];
}

- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }else{
        self.model.fileName = self.selectFile.name;
        [NoteTool update:self.model complete:^(BOOL success) {
            [self dismissViewControllerAnimated:NO completion:nil];
        }];
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImportCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ImportCell ID] forIndexPath:indexPath];
    cell.name.text = self.array[indexPath.row].name;
    cell.borderWidth = self.index == indexPath.row ? 1 : 0;
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.index = indexPath.row;
    [collectionView reloadData];
    self.selectFile = self.array[indexPath.row];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 80) / 3;
    return  CGSizeMake(w, 60.0/100*w);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

@end

